<?php

function getTeam($coachName, $leagueId) {
	$result = mysql_query("SELECT * FROM team where coachName = '" .$coachName . "' and leagueId = $leagueId");
	$team = new Team();
	
	$row = mysql_fetch_array($result);
	$team->id = $row['id'];
	$team->coachName = $row['coachName'];
	$team->teamName = $row['teamName'];
	$team->leagueId = $row['leagueId'];
	$team->teamId = $row['teamId'];
	$team->password = $row['password'];
	$team->usePreferences = $row['usePreferences'];
	$team->isAdmin = $row['isAdmin'];
	
	
	return $team;
}

function getTeamById($teamId, $isArchive) {
	$result = mysql_query("SELECT * FROM team" . getTableExtension($isArchive) . " where id = $teamId");
	
	$team = new Team();
	
	$row = mysql_fetch_array($result);
	$team->id = $row['id'];
	$team->coachName = $row['coachName'];
	$team->teamName = $row['teamName'];
	$team->leagueId = $row['leagueId'];
	$team->teamId = $row['teamId'];
	$team->password = $row['password'];
	
	
	return $team;
}

function getTeams($leagueId, $isArchive) {
	$sql = "select id, coachName, teamName, teamSubmittedStatus from team" . getTableExtension($isArchive) . " where leagueId = $leagueId";
	
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$team = new Team();
		$team->id = $row['id'];
		$team->coachName = $row['coachName'];
		$team->teamName = $row['teamName'];
		$team->teamSubmittedStatus = $row['teamSubmittedStatus'];
		
		$array[] = $team;
	}
	
	
	return $array;
}

function updateUsePreferences($teamId, $usePreferences) {
	$sql = "UPDATE team set usePreferences = $usePreferences where id = $teamId;";
	
	mysql_query($sql);
	
}

function updateTeamName($teamId, $teamName) {
	$sql = "UPDATE team set teamName = '$teamName' where id = $teamId;";
	mysql_query($sql);
	
}

function getTeamsSubmitted($round) {
	$sql = "select team from (select count(*) as count, team from aflselectedteams ast inner join player p on ast.playerId = p.id where round = $round group by team) tmp where count > 0;";
	
	$result = mysql_query($sql);
	$array = array();
	
	while($row = mysql_fetch_array($result)) {
		$array[] = $row['team'];
	}
	
	
	return $array;
}

function updateTeamSubmittedStatus($teamId) {
	$sql = "SELECT teamSubmittedStatus from team where id = $teamId;";
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	if ($row['teamSubmittedStatus'] == TeamSubmittedStatus::Not_Submitted) {
		$sql = "UPDATE team set teamSubmittedStatus = '" . TeamSubmittedStatus::Submitted . "' where id = $teamId;";
		mysql_query($sql);
	}
	
}

function insertSelectTeamPlayer($playerId, $round, $position) {
	$sql = "SELECT count(*) as count FROM aflselectedteams where playerId = $playerId and round = $round;";
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$count = $row['count'];
	
	if ($count == 0) {
		$sql = "INSERT INTO aflselectedteams (playerId, round, position) values ($playerId, $round, '$position')";
		mysql_query($sql);
	}
	
}

function checkSelectedTeamAlreadySubmitted($round, $team) {
	$sql = "SELECT count(*) as count from aflselectedteams ast inner join player p on ast.playerId = p.id where ast.round = $round and p.team = '$team'";
	$result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$count = $row['count'];
	
	return $count > 0;
}

function retrieveCoachesNotSubmittedTeams($leagueId) {
	$sql = "SELECT coachName from team where teamSubmittedStatus = '" . TeamSubmittedStatus::Not_Submitted . "' and leagueId = $leagueId";
	$result = mysql_query($sql);
	
	$teams = array();
	while($row = mysql_fetch_array($result)) {
		$teams[] = $row['coachName'];
	}
	
	return $teams;
}

function retrieveCoachIDsNotSubmittedTeams() {
	$sql = "SELECT id from team where teamSubmittedStatus = '" . TeamSubmittedStatus::Not_Submitted . "'";
	$result = mysql_query($sql);
	
	$teams = array();
	while($row = mysql_fetch_array($result)) {
		$teams[] = $row['id'];
	}
	
	return $teams;
}

function updateTeamsSubmittedStatus($leagueId) {	
	$sql = "UPDATE team set teamSubmittedStatus = '" . TeamSubmittedStatus::Submitted_Informed . "' where teamSubmittedStatus = '" . TeamSubmittedStatus::Submitted . "' and leagueId = $leagueId;";
	mysql_query($sql);
}

function getTradesForRound($teamId) {
        $sql = "SELECT tradesForRound from team where id = $teamId;";
        $result = mysql_query($sql);
	
	$row = mysql_fetch_array($result);
	$tradesForRound = $row['tradesForRound'];
	
	return $tradesForRound;
}

function updateTradesForRound($teamId) {
        $tradesForRound = getTradesForRound($teamId);
        $sql = "UPDATE team set tradesForRound = " . ($tradesForRound+1) . " where id = $teamId;";    
        mysql_query($sql);
}

function updateExtendedBench() {
	$round = getCurrentRound();
	
	$sql = "update aflselectedteams set position = 'EXB' where position = 'IC' and round = $round";
	$sql .= " and playerId in (select id from player where team in (select team from (select count(*) as numPlayers, team from aflselectedteams ast inner join player p on ast.playerid = p.id where position = 'IC' and round = $round group by team) tmp where numPlayers > 4))";
	
	mysql_query($sql);
}

?>